package com.wxl.common.http

import com.alibaba.fastjson.JSONArray
import com.lzy.okgo.model.HttpParams
import com.wxl.common.AppContext
import com.wxl.common.bean.CjqUpLoadBean
import com.wxl.common.room.entity.CollectorBean
import com.wxl.common.room.entity.DetonatorEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.DeviceUtil
import com.wxl.common.util.NetWorkUtils
import com.wxl.common.util.ObservableUtil
import com.wxl.common.util.Toll
import io.reactivex.ObservableOnSubscribe

class CommonHttpUtil {

    companion object {


        /**
         * 上传采集器版本信息
         */
        fun uploadCollectorVersion(){
            ObservableUtil.doNext(
                ObservableOnSubscribe <HttpParams>{
                    val cjqUpLoadBean = CjqUpLoadBean(
                        AppContext.appContext.getCjqTitle(),
                        AppContext.appContext.getCjqVersion()
                    )
                    val params = HttpParams()
                    params.put("actioin","updateCaiji")
                    val data = arrayListOf<CjqUpLoadBean>()
                    data.add(cjqUpLoadBean)
                    val jsonString = JSONArray.toJSONString(data)
                    params.put("list",jsonString)
                    OkGoApi.api.get().params(params).execute()
                    it.onNext(params)
                },object : ObservableUtil.Companion.OnNextCallback<HttpParams>(){
                    override fun onNext(t: HttpParams) {

                    }
                }
            )
        }


        /**
         * 获取控制器信息
         */
        fun getDetonatorInfo(callback: ObservableUtil.Companion.OnNextCallback<DetonatorEntity>){
            DBHelper.queryDetonator(object : ObservableUtil.Companion.OnNextCallback<List<DetonatorEntity>>(){
                override fun onNext(t: List<DetonatorEntity>) {
                    if(t.isNotEmpty()){
                        AppContext.appContext.saveBlastDwmc(t[0].deptid)
                        AppContext.appContext.saveBlastDwCode(t[0].dept)
                        callback.onNext(t[0])
                    } else {
                        if(NetWorkUtils.isNetWorkAvailable(AppContext.appContext.getApplication())){
                            val params = HttpParams()
                            params.put("action","getqbqInfo")
                            params.put("appversion",DeviceUtil.getVersionName(AppContext.appContext.getApplication()))
                            OkGoApi.api.get().params(params).execute(object : SerialHttpCallback<DetonatorEntity>("obj"){
                                override fun loadDataSuccess(datas: ArrayList<DetonatorEntity>) {
                                    DBHelper.clearDetonator(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                                        override fun onNext(t: Boolean) {
                                            DBHelper.insertDetonator(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                                                override fun onNext(t: Boolean) {
                                                    callback.onNext(datas[0])
                                                }
                                            },datas[0])
                                        }
                                    })
                                }

                                override fun loadDataFailed(e: Throwable) {
                                    callback.error(e)
                                }
                            })
                        } else {
                            Toll.toll("网络不可用")
                            callback.error(Exception("网络不可用"))
                        }

                    }
                }
            })

        }


        /**
         * 获取采集器信息
         */
        fun getCollectorInfo(callback: ObservableUtil.Companion.OnNextCallback<List<CollectorBean>>){
            DBHelper.queryCollector(object : ObservableUtil.Companion.OnNextCallback<List<CollectorBean>>(){
                override fun onNext(t: List<CollectorBean>) {
                    if(t.isNotEmpty()){
                        callback.onNext(t)
                    } else {
                        val params = HttpParams()
                        params.put("action","getcaijiInfo")
                        params.put("appversion",DeviceUtil.getVersionName(AppContext.appContext.getApplication()))
                        OkGoApi.api.get().params(params).execute(object : SerialHttpCallback<CollectorBean>("Obj"){
                            override fun loadDataSuccess(datas: ArrayList<CollectorBean>) {
                                DBHelper.clearInsertCollector(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                                    override fun onNext(t: Boolean) {
                                       callback.onNext(datas)
                                    }
                                },datas)
                            }

                            override fun loadDataFailed(e: Throwable) {
                                callback.error(e)
                            }
                        })
                    }
                }
            })
        }
    }
}